package com.wuyanzu.shixicoderedo.controller;

import com.wuyanzu.shixicoderedo.common.dto.FeeItemDTO;
import com.wuyanzu.shixicoderedo.common.dto.SettlementRequestDTO;
import com.wuyanzu.shixicoderedo.common.utils.ResponseVO;
import com.wuyanzu.shixicoderedo.service.SettlementService;
import com.wuyanzu.shixicoderedo.service.TokenService;
import jakarta.servlet.http.HttpServletRequest;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/settlement")
@Slf4j
public class SettlementController {

    @Autowired
    private SettlementService settlementService;

    @Autowired(required = false)
    private TokenService tokenService;

    // 映射 POST /api/settlement/inpatient
    @PostMapping("/inpatient")
    public ResponseVO inpatientSettlement(@RequestBody SettlementRequestDTO reqDto,
                                          HttpServletRequest request) {

        // 尝试从 DTO 中读取 patientId
        Long patientId = reqDto.getPatientId();

        // 若仍无 patientId，返回 400（客户端需传 patientId 或 携带合法 token）
        if (patientId == null) {
            return ResponseVO.error("400", "患者ID不能为空");
        }

        // 从 DTO 中读取 items
        List<FeeItemDTO> items = reqDto.getItems();

        // 如果业务要求必须传 items，则在此校验并返回错误（可根据业务调整）
        if (items == null || items.isEmpty()) {
            return ResponseVO.error("400", "费用项不能为空");
        }


        // 调用 Service 层处理结算逻辑
        Map<String, Object> result = settlementService.handleSettlement(patientId, items);

        // 返回统一成功响应（200 + 业务结果）
        return ResponseVO.success("200", result);

    }
}
